package com.example.demo.server.web;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.annotation.ApiDesc;
import com.example.demo.entity.base.PageRequestBean;
import com.example.demo.entity.mybatisplus.TbServerListener;
import com.example.demo.server.service.MpTbServerListenerService;
import com.example.demo.util.EmptyUtil;
import com.example.demo.util.ResponseFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 监听服务表 前端控制器
 * </p>
 *
 * @author xzw
 * @since 2021-01-18
 */
@Slf4j
@RestController
@RequestMapping("/mpapi/tbserverlistener")
public class MpTbServerListenerController {
	
	@Autowired
	private MpTbServerListenerService mpTbServerListenerService;
	
	@ApiDesc("查询服务状态")
	@RequestMapping(value="/queryserverlisten")
	public Object queryServerListen(HttpServletRequest request,@RequestBody PageRequestBean<TbServerListener> pagerequest) {
		
		int pageNum = pagerequest.getPageNum();
		
		int pageSize = pagerequest.getPageSize();
		
		TbServerListener params = pagerequest.getParams();
		
		/*Page<TbServerListener> page = new Page<>(pageNum, pageSize);
		page.setSearchCount(true);
		page.setSearchCount(true);*/
		
		QueryWrapper<TbServerListener> wrapper = new QueryWrapper<>();
		
		if(EmptyUtil.isNotEmpty(params.getCreate_time())) {
			wrapper.ge("create_time", params.getCreate_time());
		}
		
		if(EmptyUtil.isNotEmpty(params.getServer_name())) {
			wrapper.like("server_name", params.getServer_name());
		}
		
		Page<TbServerListener> page = PageHelper.startPage(pageNum, pageSize,true);
		
		List<TbServerListener> retList = mpTbServerListenerService.list(wrapper);
		log.info("总数：{}",page.getTotal());
		
		
		
		return retList;
	}
	
	@ApiDesc("添加服务监听")
	@RequestMapping(value="/addserver")
	public Object addServer(HttpServletRequest request,TbServerListener tbServerListener){
		
		tbServerListener.setCreate_time(new Date());
		
		try {
			mpTbServerListenerService.save(tbServerListener);
		} catch (Exception e) {
			log.error("添加服务监听失败",e);
			return ResponseFactory.setFail("添加服务监听失败");
		}
		
		return ResponseFactory.setSuccess("添加服务监听成功！");
	}
}

